package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper adminMapper;
    @Autowired
    private StatMapper statMapper;

    @Override
    public DtsAdmin findAdmin(String username) {

//        QueryWrapper wrapper = new QueryWrapper();
//               wrapper.eq("username", username);
//        wrapper.eq("deleted", false);

        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getUsername, username);
        wrapper.eq(DtsAdmin::getDeleted ,false);

        DtsAdmin admin = adminMapper.selectOne(wrapper);

        return admin;
    }

    @Override
    public List<Map<String, Object>> findAdminList() {
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getDeleted, false);

        List<DtsAdmin> admins = adminMapper.selectList(wrapper);

        List<Map<String,Object>> data = new ArrayList<>();

        for (DtsAdmin admin : admins) {
            Map<String,Object> map = new HashMap<>();
            map.put("value", admin.getId() );
            map.put("label", admin.getUsername());
            data.add(map);
        }
        return data;
    }

    @Override
    public void updatePassword(String newPwd, Integer id) {
        statMapper.updateAdminPwd(newPwd,id);
    }
}
